<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/head.png" alt="">
      </div>
      <!-- 表单区域 -->
      <el-form ref="loginFormRef" label-width="0px" class="login_info" :model="LoginForm" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="LoginForm.username" placeholder="请输入用户名" prefix-icon="iconfont  icon-wo">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="LoginForm.password" type="password" placeholder="请输入密码" prefix-icon="iconfont  icon-mima">
          </el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="danger" @click="login">登陆</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      LoginForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        //   校验用户名
        username: [
          { required: true, message: "请输入登陆名称", trigger: 'blur' },
          { min: 3, max: 10, message: "长度在3到10个字符", trigger: 'blur' }
        ],
        //   校验密码
        password: [
          { required: true, message: "请输入密码", trigger: 'blur' },
          { min: 6, max: 10, message: "长度在6到10个字符", trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // 重置表单
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // 表单预校验
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post('login', this.LoginForm)
        if (res.meta.status !== 200) return this.$message.error("登陆失败")
        this.$message.success("登陆成功")
        window.sessionStorage.setItem('token', res.data.token);
        this.$router.push("/home")
      })
    }
  }
}
</script>
<style  scoped>
.login_container {
  background: url(../assets/login_background_hd.jpg);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: 100%;
}
el-input {
  background-color: bisque;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: rgba(255, 255, 255, 0.055);
  border-radius: 13px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid rgba(255, 255, 255, 0.603);
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.452);
}
.avatar_box > img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: rgb(212, 212, 212);
}
.btns {
  display: flex;
  /* 居右对其 */
  justify-content: flex-end;
}
.login_info {
  position: absolute;
  bottom: 0;
  margin: 20px 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>